﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __C9309D20_1B5B_4609_BFC8_1D5ED7F68AB8__
#define __C9309D20_1B5B_4609_BFC8_1D5ED7F68AB8__

#include "guid_declare.h"
#include "com_declare.h"
#include "i_unknown.h"

namespace xos
{
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //

    int xos_internal_query_interface( void * pThis, const _XOS_COM_INTERFACE_MAP_ENTRY * pEntries, const char * iid, void ** ppv )
    {
        int result = 1;
        int ret = 0;

        void * pVoid = 0;

        if( ( 0 == ret ) && ( 0 == xos_is_equal_interface_id( iid, i_unknown_id ) ) )
        {
            pVoid = ( void* )( ( xos_int )pThis + pEntries->dw );
            ret = 1;
        }

        while( ( 0 == ret ) && ( 0 != pEntries->iid ) )
        {
            if( 0 != xos_is_equal_interface_id( iid, pEntries->iid ) )
            {
                pEntries++;
                continue;
            }
            if( 0 != pEntries->of )
            {
                pVoid = *( void** )( ( xos_int )pThis + pEntries->of );
            }
            else
            {
                pVoid = ( void* )( ( xos_int )pThis + pEntries->dw );
            }
            ret = 1;
        }

        if( ppv )
        {
            *ppv = pVoid;
        }

        if( pVoid )
        {
            result = 0;
        }

        return result;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //

    int xos_is_equal_interface_id( const char * lpszStr1, const char * lpszStr2 )
    {
        int ret = 0;

        if( !lpszStr1 || !lpszStr2 )
        {
            return -2;
        }

        if( lpszStr1 == lpszStr2 )
        {
            return 0;
        }

        while( ( 0 != ( *lpszStr1 ) ) && ( *lpszStr1 == *lpszStr2 ) )
        {
            lpszStr1++;
            lpszStr2++;
        }

        if( *lpszStr1 > *lpszStr2 )
        {
            ret = 1;
        }
        else if( *lpszStr1 < *lpszStr2 )
        {
            ret = -1;
        }

        return ret;
    }

}

#endif // __C9309D20_1B5B_4609_BFC8_1D5ED7F68AB8__
